package com.example.springboot_mail.controller;

import java.io.File;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

@RestController
public class MailController {

	@Autowired(required = false)
    private JavaMailSender jms;
	@Autowired
	private TemplateEngine templateEngine;
	
    @Value("${spring.mail.username}")
    private String from;
    
    
    String targetMail = "888888@qq.com";
    /**
     * 发送简单的邮件
     * @return
     */
    @RequestMapping("sendSimpleEmail")
    public String sendMail(){
       try{
           SimpleMailMessage message = new SimpleMailMessage();
           message.setFrom(from);
           message.setTo(targetMail);//接收邮件号
           message.setSubject("一封简单地邮件");//邮件主题
           message.setText("Hello World");//邮件内容
           jms.send(message);
           return "发送成功";
       }catch (Exception e){
           e.getStackTrace();
           return e.getMessage();
       }

    }
    /**
     * 发送HTML格式文件
     * @return
     */
    @RequestMapping("sendHtmlEmail")
    public String sendHtmlEmail(){
    	 MimeMessage message = null;
         try {
             message = jms.createMimeMessage();
           //发送邮件之间给自己抄送一份，防止网易当做垃圾邮件
             message.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(from));
             MimeMessageHelper helper = new MimeMessageHelper(message, true);
             helper.setFrom(from); 
             helper.setTo(targetMail); // 接收地址
             helper.setSubject("一封HTML格式的邮件"); // 标题
             // 带HTML格式的内容
             StringBuffer sb = new StringBuffer("<p style='color:#42b983'>使用Spring Boot发送HTML格式邮件。</p>");
             helper.setText(sb.toString(), true);
             jms.send(message);
             return "发送成功";
         } catch (Exception e) {
             e.printStackTrace();
             return e.getMessage();
         }
    	
    }
    /**
     * 发送带附件的邮件
     * @return
     */
    @RequestMapping("sendAttachmentsMail")
    public String sendAttachmentsMail() {
        MimeMessage message = null;
        try {
            message = jms.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
          //发送邮件之间给自己抄送一份，防止网易当做垃圾邮件
            message.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(from));
            helper.setFrom(from); 
            helper.setTo(targetMail); // 接收地址
            helper.setSubject("一封带附件的邮件"); // 标题
            helper.setText("详情参见附件内容！"); // 内容
            // 传入附件
            FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/file/项目文档.docx"));
            helper.addAttachment("项目文档.docx", file);
            jms.send(message);
            return "发送成功";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }
    /**
     * 发送模板邮件
     * @param code
     * @return
     */
    @RequestMapping("sendTemplateEmail")
    public String sendTemplateEmail(String code) {
        MimeMessage message = null;
        try {
            message = jms.createMimeMessage();
            //发送邮件之间给自己抄送一份，防止网易当做垃圾邮件
            message.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(from));
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from); 
            helper.setTo(targetMail); // 接收地址
            helper.setSubject("邮件摸板测试"); // 标题
            // 处理邮件模板
            Context context = new Context();
            context.setVariable("code", code);
            String template = templateEngine.process("emailTemplate", context);
            helper.setText(template, true);
            jms.send(message);
            return "发送成功";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

}
